import os
from utils import doc_util, read_config as config, json_util, date_util
from service import score_service

users_path = os.path.join(config.data_path, 'common', 'users')


# flag 1:查询当前触发 2:查询全部触发
def get_trigger_data(user_code, date, flag):
    path = os.path.join(config.data_path, 'task', 'document', '突破均线', 'stock_monitor_dict')
    monitor_dict = doc_util.get_a_doc_dict(path, 'code')
    file_dict = {}
    up_flag = 0
    user_m_dict = monitor_dict
    if user_code != '':
        if os.path.exists(os.path.join(users_path, user_code)):
            user_m_dict = doc_util.get_a_doc_dict(os.path.join(users_path, user_code), 'code')
    for code in monitor_dict.keys():
        if code not in user_m_dict.keys():
            continue
        monitor_info = json_util.info_to_json(monitor_dict[code])
        code = monitor_info['code']
        name = monitor_info['name']
        if flag == 1:
            trigger_time = monitor_info['trigger_time']
            if trigger_time[0:10] != date_util.get_date_str():  # 不是当天不显示
                continue
        result_file_path = monitor_info['result_file_path']
        if 'score_info' not in monitor_info:
            up_flag = 1
        else:
            score_info = monitor_info['score_info']
            if 'exam_time' in score_info.keys():
                exam_time = score_info['exam_time']
                if (date_util.get_timestamp_now() - exam_time) > 600:
                    up_flag = 1
            else:
                up_flag = 1
        if up_flag == 1:
            monitor_info = score_service.common_exam(monitor_info)
            monitor_dict[code] = monitor_info
        score_info = json_util.info_to_json(monitor_info['score_info'])
        if 'trigger_time' not in monitor_info.keys():
            monitor_info['trigger_time'] = '2000-01-01 00:00:00'
            monitor_info['trigger_timestamp'] = date_util.datetime2timestamp('2000-01-01 00:00:00')
        level = score_info['level']
        score = score_info['score']
        trigger_time = monitor_info['trigger_time']  # 触发时间
        result_file_path = '当前' + result_file_path
        if result_file_path not in file_dict.keys():
            file_dict[result_file_path] = [['名称', '触发时间', '等级', '备注']]
        file_list = []
        file_list.append(name)
        file_list.append(trigger_time[0:10])
        file_list.append(level)
        file_list.append('详情')
        file_dict[result_file_path].append(file_list)
    if up_flag:
        doc_util.gen_a_doc(os.path.join(config.data_path, 'task', 'document', '突破均线'),
                           'stock_monitor_dict',
                           monitor_dict)
    return file_dict


# 获取当日触发情况
# flag 1:查询当前触发 2:查询全部触发
def get_trigger_table(flag):
    file_dict = get_trigger_data('', '', flag)
    file_count = 0
    a = b''
    for key in file_dict.keys():
        file_count += 1
        if file_count % 2 == 1:
            a += b'<div class="row">'
        a += b'<div class="col-md-6">'
        a += b'<div class="white-box">'
        a += bytes('<h2 class="header-title">' + key + '</h2>', encoding='utf-8')
        a += b'<div class="table-wrap">'
        if file_count % 2 == 1:
            a += b'<table class="table table-bordered">'
        else:
            a += b'<table class="table table-striped">'
            # a += b'<table class="table table table-hover m-0">'
        file_list = file_dict[key]
        file_list_len = len(file_list)
        if file_list_len == 0:
            return ''
        else:
            file0 = file_list[0]
            a += b'<thead>'
            a += b'<tr>'
            a += bytes('<th>序号</th>', encoding='utf-8')
            for text in file0:
                a += bytes('<th>' + text + '</th>', encoding='utf-8')
            a += b'                        </tr>'
            a += b'                        </thead>'
        for i in range(1, file_list_len):
            a += b'<tbody>'
            a += b'<tr>'
            a += bytes(' <th scope="row">' + str(i) + '</th>', encoding='utf-8')
            file_n = file_list[i]
            for text in file_n:
                a += bytes('<td>' + str(text) + '</td>', encoding='utf-8')
        a += b'                        </tbody>'
        a += b'                    </table>'
        a += b'                </div>'
        a += b'            </div>'
        a += b'        </div>'
        if file_count % 2 == 0 or file_count == len(file_dict):
            a += b'</div>'
    return a


if __name__ == '__main__':
    file_dict = get_trigger_table(2)
